import httpRequest from '@/config/axios/httpRequest'
import {
  CheckEmailExistReq,
  CheckEmailExistResp,
  CheckMobileExistReq,
  CheckMobileExistResp,
  CheckUserNameExistReq,
  CheckUserNameExistResp,
  LoginUserReq,
  LoginUserResp,
  RegisterUserReq,
  RegisterUserResp,
  SmsLoginReq,
  SocialAuthRedirectReq,
  SocialAuthRedirectResp,
  SocialLoginReq,
  SocialLoginResp
} from '@/models/auth/LoginUserModel'
import userapi from '@/api/declaration/userApi'
import * as SysApi from '@/api/declaration/sysApi'
import { RefreshTokenResp } from '@/models/auth/TokenModel'
import { QueryTenantByNameReq, QueryTenantByNameRsp } from '@/models/auth/TenantInfoModel'
import {
  CaptchaReq,
  CaptchaResp,
  CheckCaptchaReq,
  CheckCaptchaResp
} from '@/models/auth/CaptchaModel'
import { GetTenantByWebsiteResp } from '@/models/auth/tenantModel'
import { GetUserPermissionResp } from '@/models/auth/PermissionModel'
import { SendSmsReq, SendSmsResp } from '@/models/system/SmsModel'
import captchaapi from '@/api/declaration/captchaApi'
import tenantapi from '@/api/declaration/tenantApi'

// 登录
export const login = async (data: LoginUserReq) => {
  return await httpRequest.post<LoginUserResp>(userapi.Login, data)
}

// 检查用户名可用性
export const checkUserName = async (params: CheckUserNameExistReq) => {
  return await httpRequest.post<CheckUserNameExistResp>(userapi.CheckUserNameExist, params)
}

// 检查手机号码可用性
export const checkMobile = async (params: CheckMobileExistReq) => {
  return await httpRequest.post<CheckMobileExistResp>(userapi.CheckMobileExist, params)
}

// 检查邮件是否可用
export const checkEmail = async (params: CheckEmailExistReq) => {
  return await httpRequest.post<CheckEmailExistResp>(userapi.CheckEmailExist, params)
}

// 注册
export const register = async (data: RegisterUserReq) => {
  return await httpRequest.post<RegisterUserResp>(userapi.Register, data)
}

// 刷新访问令牌
export const refreshToken = async (token: RegisterUserReq) => {
  return await httpRequest.post<RefreshTokenResp>(userapi.RefreshToken, token)
}

// 使用租户名，获得租户编号
export const getTenantIdByName = async (req: QueryTenantByNameReq) => {
  return await httpRequest.get<QueryTenantByNameRsp>(tenantapi.GetTenantInfoByName,{params: req})
}

// 使用租户域名，获得租户信息
export const getTenantByWebsite = async (Website: string) => {
  return await httpRequest.get<GetTenantByWebsiteResp>(userapi.GetTenantByWebsite,{
    params: {
      Website
    }
  })
}

// 登出
export const loginOut = async () => {
  return await httpRequest.post(userapi.LoginOut)
}

// 获取用户权限信息
export const getInfo = async () => {
  return await httpRequest.get<GetUserPermissionResp>(userapi.GetUserPermission)
}

//获取登录验证码
export const sendSmsCode = async (data: SendSmsReq) => {
  return await httpRequest.post<SendSmsResp>(SysApi.default.SendSms, data)
}

// 短信验证码登录
export const smsLogin = async (data: SmsLoginReq) => {
  return await httpRequest.post(userapi.SmsLogin, data)
}

// 社交快捷登录，使用 code 授权码
export const socialLogin = async (data: SocialLoginReq) => {
  return await httpRequest.post<SocialLoginResp>(userapi.SocialLogin, data)
}

// 社交授权的跳转
export const socialAuthRedirect = async (params: SocialAuthRedirectReq) => {
  return await httpRequest.get<SocialAuthRedirectResp>(userapi.SocialAuthRedirect, {
    headers: { 'Content-Type': 'application/json' },
    params
  })
}
// 获取验证图片以及 token
export const getCode = async (data: CaptchaReq) => {
  return await httpRequest.post<CaptchaResp>(captchaapi.GetCaptcha, data)
}

// 滑动或者点选验证
export const reqCheck = async (data: CheckCaptchaReq) => {
  return await httpRequest.post<CheckCaptchaResp>(captchaapi.CheckCaptcha, data)
}
